package com.example.dllo.exe.giftspeak.jingxuan;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dllo.exe.bean.JingXuanBean;
import com.example.dllo.exe.R;
import com.example.dllo.exe.bean.SQLGongLueBean;
import com.example.dllo.exe.fengzhuang.DBTool;
import com.example.dllo.exe.fengzhuang.NetTool;
import com.example.dllo.exe.giftspeak.songnvpiao.Collection;
import com.example.dllo.exe.me.MyUser;
import com.example.dllo.exe.me.ZhuCeAty;
import com.squareup.picasso.Picasso;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class JingXuanAdapter extends BaseAdapter{
    private Context context;
    private JingXuanBean jingXuanBeen;
    private Itemonclick itemonClick;

    public void setItemonClick(Itemonclick itemonClick) {
        this.itemonClick = itemonClick;
    }

    public  interface  Itemonclick{
        void  Itemonclick(int pos);
    }
    @Override
    public int getCount() {
        return jingXuanBeen!=null?jingXuanBeen.getData().getItems().size():0;
    }

    public JingXuanAdapter(Context context) {
        this.context = context;
    }

    public void setJingXuanBeen(JingXuanBean jingXuanBeen) {

        this.jingXuanBeen = jingXuanBeen;
notifyDataSetChanged();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder=null;
        LayoutInflater inflater=LayoutInflater.from(context);
        if(view==null){
            view=inflater.inflate(R.layout.jingxuanframentlayout_layout,null);
            viewHolder=new ViewHolder(view);
            view.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) view.getTag();
        }
        final JingXuanBean jingXuanBeans=jingXuanBeen;
        viewHolder.descriptiontext.setText(jingXuanBeans.getData().getItems().get(i).getTitle());
        try {
            viewHolder.titletexView.setText(jingXuanBeans.getData().getItems().get(i).getColumn().getTitle());
            viewHolder.categorytext.setText( jingXuanBeans.getData().getItems().get(i).getColumn().getCategory());

            viewHolder.likesTextView.setText(jingXuanBeans.getData().getItems().get(i).getLikes_count()+"");
            viewHolder.nicknameText.setText(jingXuanBeans.getData().getItems().get(i).getAuthor().getNickname());

            NetTool netTool=new NetTool();
            netTool.getImage(jingXuanBeans.getData().getItems().get(i).getCover_image_url(),viewHolder.imageImagview);
            Picasso.with(context).load(jingXuanBeans.getData().getItems().get(i).getAuthor().getAvatar_url()).into(viewHolder.authorImage);
            final ViewHolder finalViewHolder = viewHolder;
            viewHolder.imageViewxinxing.setOnClickListener(new View.OnClickListener() {
               private SQLGongLueBean sqlGongLueBean;
               @Override
               public void onClick(View view) {
                   MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
                   if (myUser != null) {
                       sqlGongLueBean= new SQLGongLueBean();



                       sqlGongLueBean.setName(jingXuanBeans.getData().getItems().get(i).getAuthor().getNickname());
                       sqlGongLueBean.setTitle(jingXuanBeans.getData().getItems().get(i).getTitle());
                       sqlGongLueBean.setUrl(jingXuanBeans.getData().getItems().get(i).getCover_image_url());
                       DBTool.getInstance().insertStudent(sqlGongLueBean);



                       ScaleAnimation sa = new ScaleAnimation(0, 1.0f, 0, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                       sa.setDuration(2000);
                       sa.setRepeatCount(0);
                       finalViewHolder.imageViewxinxing.setImageResource(R.mipmap.muchlike);
                       finalViewHolder.imageViewxinxing.startAnimation(sa);
                       Toast.makeText(context.getApplicationContext(), "喜欢添加成功", Toast.LENGTH_SHORT).show();

                       Collection collection = new Collection();
                       collection.setName(
                               jingXuanBeans.getData().getItems().get(i).getAuthor().getNickname());
                       collection.setTitle(jingXuanBeans.getData().getItems().get(i).getTitle());
                       collection.setUrl(jingXuanBeans.getData().getItems().get(i).getCover_image_url());


                       collection.save(new SaveListener<String>() {
                           @Override
                           public void done(String s, BmobException e) {
                               if (e == null) {
                                   Log.d("SongNvPiaoAdapter", "有数据");
                               } else {
                                   Log.d("SongNvPiaoAdapter", "没有数据");
                               }
                           }
                       });
                   } else {
                       Intent intent = new Intent(context.getApplicationContext(), ZhuCeAty.class);
                       context.startActivity(intent);
                   }

               }
           });
        } catch (NullPointerException e) {
            Log.d("JingXuanAdapter", "没有数据" + i);
        }
          viewHolder.imageImagview.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  itemonClick.Itemonclick(i);
              }
          });

//        fit()将图片进行压缩
        ObjectAnimator.ofFloat(view,"translationY",400,0).setDuration(1000).start();
        ObjectAnimator.ofFloat(view,"rotationX",360,0).setDuration(1000).start();
        return view;
    }
    public  class  ViewHolder{
        TextView titletexView;
        ImageView  imageImagview;
        TextView likesTextView;
       TextView categorytext;
        ImageView authorImage;
        TextView nicknameText;
        TextView  descriptiontext;
        ImageView imageViewxinxing;
        public  ViewHolder(View view){
            titletexView= (TextView) view.findViewById(R.id.title_text);
            imageImagview= (ImageView) view.findViewById(R.id.jingxuan_ImageImageView);
            likesTextView= (TextView) view.findViewById(R.id.jingxuan_likescountTextView);
            categorytext= (TextView) view.findViewById(R.id.category_text);
            descriptiontext= (TextView) view.findViewById(R.id.jingxuan_descriptionTextView);
            authorImage= (ImageView) view.findViewById(R.id.avatar_image);
            nicknameText= (TextView) view.findViewById(R.id.nickname_text);
            imageViewxinxing= (ImageView) view.findViewById(R.id.image_xinxing);
        }
    }


}
